<?php
	class LoginView
	{
		// Membervariables
		private $_navView = null;
		
		// Constants
		const LOGIN = "Login";
		const LOGOUT = "Logout";
		const REMEMBER = "Remember";
		const DELETE = "Action";

		public function __construct(\NavigationView $navView)
		{
			$this->_navView = $navView;
		}
			
		// Generate the Login box.
		public function DoLoginBox()
		{
			return "<form method='post' action='" . $this->_navView->GetFormActionLink() . "'>" .
				"<fieldset style='width: 210px'>" .
					"Name: <input id='username' name='" . UserView::USERNAME . "' type='text' /><br />" .
					"Password: <input id='password' name='" . UserView::PASSWORD . "' type='password' /><br />" .
					"<input id='login' name='" . LoginView::LOGIN . "' type='submit' value='Login' />" .
					" Remember me:<input id='remember' name='" . LoginView::REMEMBER . "' type='checkbox' value='Remember' />" .
				"</fieldset>" .
			"</form>";
		}

		// Generate the logout-button. 
		public function DoLogoutButton()
		{
			return "<form method='post' action='" . $this->_navView->GetFormActionLink() ."'>" .
				"<input id='logout' name='" . LoginView::LOGOUT . "' type='submit' value='Logout' />" .
			"</form>";
		}
		
		// Returns the username.
		public function GetUserName()
		{
			if (isset($_COOKIE[UserView::USERNAME]) == true)
			{
				return $_COOKIE[UserView::USERNAME];
			}
			return $_POST[UserView::USERNAME];
		}
		
		// Returns the password 
		public function GetPassword()
		{
			if (isset($_COOKIE[UserView::PASSWORD]) == true)
			{
				return $_COOKIE[UserView::PASSWORD];
			}
			return $_POST[UserView::PASSWORD];
		}
		
		// Logs the login message.
		public function LoginMessage()
		{
			Log::LogMessage(LoginHandler::LOGINMESSAGE);
		}
			
		// Logs the logout message.
		public function LogoutMessage()
		{
			Log::LogMessage(LoginHandler::LOGOUTMESSAGE);
		}
		
		// Logs the loginerrormessage.
		public function LoginErrorMessage()
		{
			Log::LogMessage(LoginHandler::LOGINERRORMESSAGE);
		}		
		
		// Returns wether the user tried to log in or not.
		public function TriedToLogin()
		{			
			// Check if the login button has been pressed.
			if (isset($_POST[LoginView::LOGIN]) == true)
			{
				return true;
			}				
			// Otherwise return false.
			return $this->IsCookieSet();
		}
		
		// Returns wether the user tried to log out or not.
		public function TriedToLogout()
		{
			// Check if the logout button has been pressed.
			if (isset($_POST[LoginView::LOGOUT]) == true)
			{
				return true;
			}
			
			// Otherwise return false;
			return false;
		}
		
		// Set the value of the cookie.
		public function SetCookie()
		{
			setcookie(UserView::USERNAME, $this->GetUserName(), time()+60*60*24*30);
			setcookie(UserView::PASSWORD, $this->GetPassword(), time()+60*60*24*30);
		}
		
		// Sets the current users username in the session.
		public function SetUsernameInSession()
		{
			$_SESSION[UserView::USERNAME] = $this->GetUserName();
		}
		
		// Unset the value of the cookie.
		public function UnSetCookie()
		{
			setcookie(UserView::USERNAME, "", time()-3600);
			setcookie(UserView::PASSWORD, "", time()-3600);
		}
		
		// Returns wether the cookie has been set.
		public function IsCookieSet()
		{
			if (isset($_COOKIE[UserView::USERNAME]) == true && isset($_COOKIE[UserView::PASSWORD]) == true)
			{
				return true;
			}
			
			return false;	
		}
		
		// Returns wether the user checked the box to remember the login.
		public function RememberLogin()
		{
			if (isset($_POST[LoginView::REMEMBER]) == true)
			{
				return true;	
			}
			
			return false;
		}		
	}